package com.matace.keychain.presentation.view;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.Observer;

import com.google.android.material.snackbar.Snackbar;
import com.matace.keychain.R;
import com.matace.keychain.data.UserRepositoryImpl;
import com.matace.keychain.domain.LoginUseCase;
import com.matace.keychain.presentation.viewmodel.LoginViewModel;

/**
 * 登录活动
 * 负责用户登录界面的显示和交互逻辑
 */
public class LoginActivity extends AppCompatActivity {
    private EditText editTextUsername;
    private EditText editTextPassword;
    private Button buttonLogin;
    private TextView textViewForgotPassword;
    private TextView textViewRegister;
    private CheckBox checkBoxRememberMe;
    private LoginViewModel loginViewModel;

    /**
     * 活动创建时调用
     * 初始化视图、ViewModel和事件监听器
     * @param savedInstanceState 保存的实例状态
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        // 初始化视图
        editTextUsername = findViewById(R.id.editTextUsername);
        editTextPassword = findViewById(R.id.editTextPassword);
        buttonLogin = findViewById(R.id.buttonLogin);
        textViewForgotPassword = findViewById(R.id.textViewForgotPassword);
        textViewRegister = findViewById(R.id.textViewRegister);
        checkBoxRememberMe = findViewById(R.id.checkBoxRememberMe);

        // 初始化ViewModel
        loginViewModel = new ViewModelProvider(this, new ViewModelProvider.Factory() {
            @Override
            public <T extends androidx.lifecycle.ViewModel> T create(Class<T> modelClass) {
                if (modelClass.isAssignableFrom(LoginViewModel.class)) {
                    return (T) new LoginViewModel(new LoginUseCase(new UserRepositoryImpl(LoginActivity.this)));
                }
                throw new IllegalArgumentException("Unknown ViewModel class");
            }
        }).get(LoginViewModel.class);

        // 观察ViewModel中的数据变化
        loginViewModel.getUsername().observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                editTextUsername.setText(s);
            }
        });

        loginViewModel.getPassword().observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                editTextPassword.setText(s);
            }
        });

        loginViewModel.getRememberMe().observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                checkBoxRememberMe.setChecked(aBoolean);
            }
        });

        loginViewModel.getLoginSuccess().observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean success) {
                if (success) {
                //    Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
//                    Snackbar.make(findViewById(android.R.id.content), "登录成功", Snackbar.LENGTH_SHORT).show();
//                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    Intent intent = new Intent(LoginActivity.this, TermsActivity.class);
                    startActivity(intent);
                    finish();
                }
            }
        });

        loginViewModel.getLoginError().observe(this, new Observer<String>() {
            @Override
            public void onChanged(String error) {
                if (!error.isEmpty()) {
                    Toast.makeText(LoginActivity.this, error, Toast.LENGTH_SHORT).show();
                }
            }
        });

        // 设置点击事件监听器
        buttonLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loginViewModel.setUsername(editTextUsername.getText().toString());
                loginViewModel.setPassword(editTextPassword.getText().toString());
                loginViewModel.setRememberMe(checkBoxRememberMe.isChecked());
                loginViewModel.login();
            }
        });

        textViewRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });

        textViewForgotPassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO: 实现忘记密码功能
                Toast.makeText(LoginActivity.this, "忘记密码功能待实现", Toast.LENGTH_SHORT).show();
            }
        });
    }
}